﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Model;
using Manager;
using System.Threading;
namespace Manager.Helper
{
    public class ResultDicCache
    {
        public static DateTime FlageDate = DateTime.Now.Date.AddDays(-1);
        public static HW_ItemResultManager manager = new HW_ItemResultManager();
        public static List<HW_ItemResult> ItemDic = new List<HW_ItemResult>();
        public static bool isrun = false;
        public static List<HW_ItemResult> GetItemDic()
        {
            if(ItemDic.Count==0)
            {
                ItemRefresh();
            }
            return ItemDic;
        }
        public static void ItemRefresh()
        {
            ItemDic.Clear();
            ItemDic=manager.GetList().ToList();
        }
        public static void Clear()
        {
            ItemDic.Clear();
        }
        public static void OnStart(object sender,EventArgs e)
        {
            Thread thread = new Thread(new ThreadStart(FreshResultDicCache));
            thread.Start();
    }
        public static void FreshResultDicCache()
        {
            while (true)
            {
                if (FlageDate != DateTime.Now.Date)
                {
                    ResultDicCache.ItemRefresh();
                    if (!isrun)
                    {
                        CountYearResult();
                    }
                    FlageDate = DateTime.Now.Date;
                }
                Thread.Sleep(1000 * 60 * 6);
            }
        }
        public static void CountYearResult()
        {
            try
            {
                ResultManager manager = new ResultManager();
                isrun = true;
                manager.CountYearResult();
                isrun = false;
            }
            catch
            {
            }
            finally
            {
                isrun = false;
            }
        }
    }
}
